<template>
  <div id="app">
    <div class="container">
      <div class="close"><span class="iconfont iconicon-test"></span></div>
      <div class="logo"><span class="iconfont iconnew"></span></div>
      <div class="inputs">
        <myinput
          :rules="/^(\d{5,6})$|^(1\d{10})$/"
          msg="请输入合法的手机号"
          placeholder="请输入手机号"
          v-model="user.username"
        ></myinput>

        <myinput
          placeholder="密码"
          v-model="user.password"
          type="password"
          @keyup.enter.native="login"
        ></myinput>
      </div>
      <p class="tips">
        没有账号？
        <a href="#/register" class="">去注册</a>
      </p>
      <mybutton type="success" @click="login">登录</mybutton>
    </div>
  </div>
</template>

<script>
import mybutton from "@/components/mybutton.vue";
import myinput from "@/components/myinput.vue";
import { userLogin } from "@/api/user.js";

export default {
  data() {
    return {
      user: {
        username: "11111111000",
        password: "123456",
      },
    };
  },
  components: {
    mybutton,
    myinput,
  },
  methods: {
    login(e) {
      //发送请求
      userLogin(this.user)
        .then((res) => {
          console.log(res);
          if (res.data.message === "登录成功") {
            //将token存储到本地
            localStorage.setItem("userKey", res.data.data.token);
            //将id保存到本地
            localStorage.setItem("userId", res.data.data.user.id);
            //跳转
            //判断是否有传入参数地址

            // this.$router.push({ path: `/mypersonal/${res.data.data.user.id}` });
            let dest = location.href.split("=")[1];
            console.log(dest);
            if (dest) {
              location.href = decodeURIComponent(dest);
            } else {
              this.$router.push({
                path: `/mypersonal/${res.data.data.user.id}`,
              });
            }
          }
          this.$toast(res.data.message);
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}

.close {
  span {
    font-size: 27 / 360 * 100vw;
  }
}

.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: 126 / 360 * 100vw;
    color: #d81e06;
  }
}

.inputs {
  input {
    margin-bottom: 20px;
  }
}

.tips {
  text-align: right;
  margin-bottom: 20px;

  a {
    color: #3385ff;
  }
}
</style>